package com.framework.utils.pay.youfupay.method;

import com.alibaba.fastjson.JSONObject;
import com.framework.entity.sys.SysPayment;
import com.framework.exception.RRException;
import com.framework.utils.pay.suixingfu.horfu.util.Base64Utils;
import com.framework.utils.pay.suixingfu.horfu.util.DESUtils;
import com.framework.utils.pay.suixingfu.horfu.util.JsonUtils;
import com.framework.utils.pay.suixingfu.horfu.util.RSAUtils;
import com.framework.utils.pay.youfupay.entity.PaymentTransQueryModle;
import com.framework.utils.pay.youfupay.entity.RequestMessage;
import com.framework.utils.pay.youfupay.util.Constant;
import com.framework.utils.pay.youfupay.util.HttpClientHelper;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 付款查询
 *
 * @author Admin
 */
@Slf4j
public class PaymentQueryMethod {

    //static String url = "http://api.clooudnext.com:8280/pctrans-business/clientBusiness/common.do";
    static String url = "http://api.youfupingtai.com/clientBusiness/common.do";
    //static String url = "http://apitest.youfupingtai.com/pctrans-business/clientBusiness/common.do";
    public static PaymentTransQueryModle paymentQuery(SysPayment payment, String orderId,String batchId, String secretKey) {
        RequestMessage rm = new RequestMessage();
        rm.setReqId("req" + System.currentTimeMillis());
        rm.setFunCode("6002");
        rm.setMerId(payment.getClientId());
        rm.setVersion("V1.0");
        RequestMessage requestMessage;
        try {

            String json = getJson(payment, orderId,batchId);
            byte[] bs = DESUtils.encrypt(json.getBytes(Constant.CHARSET), secretKey);
            String reqDataEncrypt = Base64Utils.encode(bs);
            rm.setReqData(reqDataEncrypt);
            rm.setSign(RSAUtils.sign(reqDataEncrypt, payment.getPrivateKey()));
            String reqStr = JsonUtils.toJson(rm);
            Map<String, Object> map = new HashMap<>();
            map.put("reqJson", reqStr);
            String resData = HttpClientHelper.httpClientPost(url, map, Constant.CHARSET);
            System.out.println("resData:" + resData);
            requestMessage = JsonUtils.fromJson(resData, RequestMessage.class);
        } catch (Exception e) {
            throw new RRException("9000", e.getMessage());
        }
        if (!Constant.SUCCESS.equals(requestMessage.getResCode())) {
            log.info("订单号:"+orderId+"订单查询异常:" + requestMessage.getResMsg());
            throw new RRException("9000", requestMessage.getResMsg());
        }
        String detailData = null;
        try {
            byte[] base64bs = Base64Utils.decode(requestMessage.getResData());
            byte[] debs = DESUtils.decrypt(base64bs, secretKey);
            detailData = new String(debs, Constant.CHARSET);
        } catch (Exception e) {

        }
        //System.out.println(detailData);

        PaymentTransQueryModle paymentTransQueryModle = JSONObject.parseObject(detailData, PaymentTransQueryModle.class);


        return paymentTransQueryModle;

    }

    private static String getJson(SysPayment payment, String orderId, String batchId) {
        PaymentTransQueryModle ptq = new PaymentTransQueryModle();
        ptq.setMerBatchId(batchId);
        ptq.setMerId(payment.getClientId());
        List<PaymentTransQueryModle.QueryItems> list = new ArrayList<>();
        PaymentTransQueryModle.QueryItems pi1 = new PaymentTransQueryModle.QueryItems();
        pi1.setMerOrderId(orderId);
        list.add(pi1);
        ptq.setQueryItems(list);
        String json = JsonUtils.toJson(ptq);

        return json;
    }


}
